package com.WaVel.service.impl;

import com.Emplyment.dto.ArticleDto;
import com.Emplyment.dto.ArticlePageQueryDto;
import com.Emplyment.entity.Article;
import com.Emplyment.exception.RegisterException;
import com.Emplyment.result.PageResult;
import com.Emplyment.vo.ArticleVo;
import com.WaVel.mapper.ArticleMapper;
import com.WaVel.service.ArticleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;


    /**
     * 公司分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(ArticlePageQueryDto dto) {

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<ArticleVo> page = articleMapper.page(dto);
        int total = (int)page.getTotal();
        PageResult pageResult = PageResult.builder()
                .total(total)
                .items(page.getResult())
                .build();
        return pageResult;
    }



    /**
     * 添加公司数据
     * @param dto
     */
    @Override
    public Article saveEmplyment(ArticleDto dto) {
        if (dto == null){
            throw new RegisterException("公司数据添加错误");
        }
        Article article = articleMapper.SelectEmplyee(dto.getTitle(), dto.getCategoryId());
        if (article != null){
            throw new RegisterException("已有公司数据");
        }

        Article article1 = article.builder()
                        .title(dto.getTitle())
                        .content(dto.getContent())
                        .coverImg(dto.getCoverImg())
                        .state(dto.getState())
                        .collectNumber(0)
                        .website(dto.getWebsite())
                        .categoryId(dto.getCategoryId())
                        .build();

        articleMapper.SaveEmplyee(article1);
        return article1;
    }

    @Override
    public Article updateEmplyment(ArticleDto dto) {
        if (dto == null) {
            throw new RegisterException("公司数据添加错误");
        }
        Article article = new Article();
        BeanUtils.copyProperties(dto, article);
        articleMapper.updateEmplyee(article);
        return article;
    }

    @Override
    public void deleteEmplyment(Long id) {
        articleMapper.deleteEmplyee(id);
    }
}
